//Source file: C:\\javier\\mario\\Java\\taladro\\maquina\\Mesa.java

package taladro.maquina;

import java.util.*;
import taladro.control.ControlMotor;

/**
 * <p>
 * </p>
 */
public class Mesa {
    
    /**
     * /////////////////////////////////////
     * attributes
     * <p>
     * Represents ...
     * </p>
     */
    private double resolCuerdaX;
    
    /**
     * <p>
     * Represents ...
     * </p>
     */
    private double resolCuerdaY;
    
    /**
     * /////////////////////////////////////
     * associations
     * <p>
     * </p>
     */
    protected Motor motores[];
    public ControlMotor contMotores[];
    
    /**
     * @param resX
     * @param resY
     * @param motorX
     * @param motorY
     * @roseuid 3F3744F50339
     */
    public Mesa(double resX, double resY, Motor motorX, Motor motorY) {
        resolCuerdaX = resX;
        resolCuerdaY = resY;
        
        motores = new Motor[2];
        contMotores = new ControlMotor[2];
        contMotores[0] = new ControlMotor(resolCuerdaX, motorX);
        contMotores[1] = new ControlMotor(resolCuerdaY, motorY);
    }
    
    /**
     * /////////////////////////////////////
     * operations
     * <p>
     * Does ...
     * </p><p>
     * @param x ...
     * </p><p>
     * @param y ...
     * </p><p>
     * </p>
     * @throws java.lang.InterruptedException
     * @roseuid 3F341CC5025D
     */
    public void mover(double x, double y) throws java.lang.InterruptedException {
        contMotores[0].mover(x);
        contMotores[1].mover(y);
        contMotores[0].join();
        contMotores[1].join();
    }
    
    public void moverEnLineaRecta(double x, double y) throws java.lang.InterruptedException {
        double posActX = contMotores[0].getPosActual();
        double posActY = contMotores[1].getPosActual();
        double movX = 0;
        
        if( x == posActX  || y == posActY){
            mover( x,y );
            return;
        }
        
        if( x > posActX  ){
            movX = contMotores[0].getDespPaso() * 40;
        } else
            movX = contMotores[0].getDespPaso() * -40;
        
        double m = (y - posActY) / (x - posActX);
        double c =  -1.0 * (posActX * m) + posActY;
        
        double posCicloY = 0;
        double posCicloX = 0;
        
        if( movX > 0  ){
            while( contMotores[0].getPosActual() < x  ){
                if( (contMotores[0].getPosActual() + movX) > x  ){
                    posCicloX = x;
                    posCicloY = y;
                    mover(posCicloX,posCicloY);
                    return;
                } else{
                    posCicloX = contMotores[0].getPosActual() + movX;
                    posCicloY = calcPosY( m, posCicloX,c );
                }
                mover(posCicloX,posCicloY);
            }
        }
        
        while( contMotores[0].getPosActual() > x  ){
            if( (contMotores[0].getPosActual() + movX) < x  ){
                posCicloX = x;
                posCicloY = y;
                mover(posCicloX,posCicloY);
                return;
            } else{
                posCicloX = contMotores[0].getPosActual() + movX;
                posCicloY = calcPosY( m, posCicloX,c );
            }
            mover(posCicloX,posCicloY);
        }
    }

    public void moverEnLineaRecta2(double x, double y) throws java.lang.InterruptedException {
        double posActX = contMotores[0].getPosActual();
        double posActY = contMotores[1].getPosActual();
        double despPasoX = contMotores[0].getDespPaso();
        double despPasoY = contMotores[1].getDespPaso();
                
        if( x == posActX  || y == posActY){
            mover( x,y );
            return;
        }
        
        int despX = 0;
        double pendiente = ( posActY - y ) / ( posActX - x );
        
        if( x >= posActX)
           despX = 1;
        else
           despX= -1;
        
        long pasosMoverX = (long)Math.round( Math.abs( ( x - posActX ) / contMotores[0].getDespPaso() ));
        long pasosMoverY = (long)Math.round( Math.abs( ( y - posActY ) / contMotores[1].getDespPaso() ));
        long i=0;
        
        int movX=0;
        int movY=0;
        double resY=0;
        
        boolean lastY = false;
        boolean lastX = true;
        
        if( Math.abs( pendiente ) < 1 )
            lastY = true; //First X
        
        for( i=0; i < pasosMoverX; i++)
        {
            movX = despX;
            contMotores[0].moverPasos(movX);
            
            movY = (int)Math.round( pendiente + resY );
            resY = pendiente + resY - movY;
            //System.out.println( "Por mover x=" + movX + " y=" + movY );
            contMotores[1].moverPasos(movY);
            contMotores[0].join();
            contMotores[1].join();
        }
        
    }

    
    
    private double calcPosY( double m, double x, double c  ){
        return m * x + c;
    }
    
    /**
     * end mover
     * <p>
     * Does ...
     * </p><p>
     * </p>
     * @throws java.lang.InterruptedException
     * @roseuid 3F341CC50272
     */
    public void moverPuntoCero() throws java.lang.InterruptedException {
        contMotores[0].moverPosCero();
        contMotores[1].moverPosCero();
        contMotores[0].join();
        contMotores[1].join();
    }
    
    /**
     * @param indice
     * @param motor
     * @roseuid 3F37449902E6
     */
    public void setMotor(int indice, Motor motor) {
        if( indice < 2 && indice > 0 )
            motores[indice] = motor;
    }
    
    /**
     * @param indice
     * @return taladro.maquina.Motor
     * @roseuid 3F3744B602F2
     */
    public Motor getMotor(int indice) {
        if( indice < 2 && indice > 0 )
            return motores[indice];
        return null;
    }
    
    /**
     * @return double
     * @roseuid 3F374B8903C6
     */
    public double getPosActualX() {
        return contMotores[0].getPosActual();
    }
    
    /**
     * @return double
     * @roseuid 3F374B9A0321
     */
    public double getPosActualY() {
        return contMotores[1].getPosActual();
    }
    
    /**
     * @roseuid 3F37E5ED026E
     */
    public void cancelar() throws java.lang.SecurityException {
        contMotores[0].interrupt();
        contMotores[1].interrupt();
    }
}
/**
 *
 *
 *
 *  end Mesa
 *
 *
 */
